package com.myzhouye.business.controller;/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */

import com.myzhouye.business.bean.R;
import com.myzhouye.business.entity.TaskNodeConfig;
import com.myzhouye.business.service.ITaskInfoService;
import com.myzhouye.business.sse.server.SseServer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;

/**
 * @program: maple-main
 * @description:
 * @author: 那条蠢鱼
 * @create: 2024-01-04 13:16
 **/

@Slf4j
@RestController
@RequestMapping("task")
@CrossOrigin(origins ="*" )
@RequiredArgsConstructor
public class TaskController {

    private final ITaskInfoService taskInfoService;
    private final SseServer sseServer;

    @GetMapping("/{id}/node/list")
    public R nodeList(@PathVariable String id){
        List<TaskNodeConfig> nodeConfig = taskInfoService.getNodeConfig(id);
        return R.ok(nodeConfig);
    }


    @GetMapping("conn/{groupId}")
    public SseEmitter connect(@PathVariable String groupId,String uid){

        SseEmitter connect = sseServer.createConnect(uid, groupId);
        //推送当前状态
        taskInfoService.pushStatus(connect,groupId);
        return connect;
    }
}
